/*
 * @Descripttion: 
 * @Author: Silence
 * @Date: 2023-06-02 23:01:59
 * @LastEditors: Silence
 * @LastEditTime: 2023-06-05 09:36:47
 */
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  // 仓库
  state: {
    list: []
  },
  getters: {
    // 类似于compute属性
  },
  mutations: {
    // 对state的数据进行修改(唯一能修改仓库数据的地方)
    ChangeList (state, data) {
      state.list = data
    }
  },
  actions: {
    async render ({ commit }) {
      const res = await axios.get('http://ajax-api.itheima.net/api/books')
      console.log(res, 111)
      commit('ChangeList', res.data.data)
    },
    async SubmitData (context, obj) {
      console.log(obj)
      return new Promise(async (reslove, reject) => {
        if (obj.id) {
          const res = await axios.put('http://ajax-api.itheima.net/api/books/' + obj.id, { 'bookname': obj.bookname, 'author': obj.author, 'publisher': obj.publisher })
        } else {
          const res = await axios.post('http://ajax-api.itheima.net/api/books', { 'bookname': obj.bookname, 'author': obj.author, 'publisher': obj.publisher })
        }
        // context.dispatch('render')
        reslove('1')
      })
    },
    DelData ({ commit }, id) {
      // console.log(id, 333)
      return new Promise(async (reslove, reject) => {
        axios.delete('http://ajax-api.itheima.net/api/books/' + id)
        // commit('render')
        reslove('1')
      })
    }
  },
  modules: {
    // 进行模块化
  }
})
